package com.customization.assets.util;

import com.customization.commons.LogTool;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.soa.workflow.request.*;
import weaver.workflow.workflow.WorkflowVersion;

import java.util.List;
import java.util.Map;

/**
 * @author	Huangwm
 * @description	用于发起设备报废流程
 */
public class NewCreateSbbf {
    private LogTool log = new LogTool("/log/assetlog/huangwm/NewCreateSbbf",false);
    private static DBTools dbTools = new DBTools();
    private static DateTools dateTools = new DateTools();

    /**
     * 设置提醒流程流转信息
     * @param creater 创建人
     * @param title 标题
     * @param mainMap 主表数据
     * @return RequestInfo
     * @throws Exception
     */
    public RequestInfo setRequestInfo(String workflowid, String creater, String title, Map<String, String> mainMap, List<Map<String, String>> detailList) throws Exception{
        RequestInfo requestInfo = new RequestInfo();
        String activeWFID = WorkflowVersion.getActiveVersionWFID(workflowid);
        log.writeLog("获取到的活动版本workflowid  "+activeWFID);
        requestInfo.setWorkflowid(activeWFID);
        log.writeLog("设置提交到下一节点，创建人"+creater);
        requestInfo.setIsNextFlow("1");
        requestInfo.set_Remark("系统自动发起");
        requestInfo.setCreatorid(creater);
        requestInfo.setDescription(title);
        requestInfo.setMainTableInfo(setMainTableInfo(mainMap));
        requestInfo.setDetailTableInfo(setDetailTable(detailList));
        return requestInfo;
    }

    /**
     * 设置主表字段
     * @param fieldName
     * @param fieldValue
     * @return
     */
    public Property setMainField(String fieldName, String fieldValue){
        Property field = new Property();
        field.setName(fieldName);
        field.setValue(fieldValue);
        return field;
    }

    /**
     * 设置明细表字段
     * @param fieldName
     * @param fieldValue
     * @return
     */
    public Cell setDetailField(String fieldName, String fieldValue){
        Cell field = new Cell();
        field.setName(fieldName);
        field.setValue(fieldValue);
        return field;
    }

    /**
     * 设置归还流程主表数据
     * @param params
     * @return
     */
    public MainTableInfo setMainTableInfo(Map<String, String> params){
        log.writeLog("添加主表信息开始");
        log.writeLog(params.toString());
        log.writeLog("isNotDepContent="+ Util.null2String(params.get("isNotDepContent"))+"  depIsDelete="+Util.null2String(params.get("depIsDelete")));
        MainTableInfo mainTableInfo = new MainTableInfo();
        mainTableInfo.addProperty(setMainField("sqr", Util.null2String(params.get("sqr"))));//申请人
        mainTableInfo.addProperty(setMainField("sqrq",dateTools.getCurrentDate()));//申请日期
        mainTableInfo.addProperty(setMainField("szks",dbTools.getDepid(Util.null2String(params.get("sqr")))));//所在科室
        mainTableInfo.addProperty(setMainField("szcs",dbTools.getSubid(Util.null2String(params.get("sqr")))));//所在处室
        mainTableInfo.addProperty(setMainField("isNotDepContent",Util.null2String(params.get("isNotDepContent"))));
        mainTableInfo.addProperty(setMainField("depIsDelete",Util.null2String(params.get("depIsDelete"))));
        mainTableInfo.addProperty(setMainField("dbyy","人员交流变动调整"));//申请日期
        log.writeLog("主表信息添加完成");
        return mainTableInfo;
    }

    /**
     * 设置流程明细表数据
     * @param detailList
     * @return
     */
    public DetailTable setDetailTableInfo(List<Map<String, String>> detailList){
        log.writeLog("添加明细表1");
        DetailTable detailTable = new DetailTable();
        detailTable.setId("1");
        if(detailList.size()>0){
            for (Map<String, String> map : detailList) {
                Row row = new Row();
                row.addCell(setDetailField("kdbzc", Util.null2String(map.get("kdbzc"))));//实物表数据id
                row.addCell(setDetailField("zcbh", Util.null2String(map.get("zcbh"))));//实物资产编号
                row.addCell(setDetailField("lx", Util.null2String(map.get("lx"))));//设备类型
                row.addCell(setDetailField("pp", Util.null2String(map.get("pp"))));//品牌id
                row.addCell(setDetailField("ggxh", Util.null2String(map.get("ggxh"))));//规格型号id
                row.addCell(setDetailField("dqzrr", Util.null2String(map.get("dqzrr"))));//当前责任人
                row.addCell(setDetailField("syks", Util.null2String(map.get("syks"))));//责任科室
                row.addCell(setDetailField("szcs", Util.null2String(map.get("dybm"))));//责任部门
                row.addCell(setDetailField("ybmgly", getAssetsAdmin(Util.null2String(map.get("dybm")))));//原部门管理员
                row.addCell(setDetailField("gbkph", Util.null2String(map.get("gbkph"))));//关保卡片号
                row.addCell(setDetailField("dj", Util.null2String(map.get("dj")).equals("")?"0.00":Util.null2String(map.get("dj"))));//单价

                detailTable.addRow(row);
            }
        }
        return detailTable;
    }

    /**
     * 添加流程明细数据到明细表
     * @param detailList
     * @return
     */
    public  DetailTableInfo setDetailTable(List<Map<String, String>> detailList){
        DetailTableInfo detailTableInfo = new DetailTableInfo();
        if(detailList.size()>0){
            detailTableInfo.setDetailTable(new DetailTable[]{
                    setDetailTableInfo(detailList)
            });
        }
        log.writeLog("明细表1设置完成");
        return detailTableInfo;
    }

    /**
     * 获取部门所在的部门管理员
     * @param bmid
     * @return
     */
    public String getAssetsAdmin(String bmid){
        RecordSet rs = new RecordSet();
        String assetsAdmin = "";
        String sql = "select swzcgly from Matrixtable_1 where id="+bmid;
        rs.execute(sql);
        while(rs.next()){
            assetsAdmin = rs.getString("swzcgly");
        }
        return assetsAdmin;
    }
}
